//
//  SecondViewController.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/9/17.
//  Copyright © 2018年  zhaotongyue. All rights reserved.
//

import UIKit
import Charts
import Gifu
import MJRefresh
import SwiftyUserDefaults


//import AAInfographics
extension SecondViewController: IAxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return self.bottomArr![Int(value) % self.bottomArr!.count] as! String
    }
}


class SecondViewController: BaseViewController ,UITableViewDelegate, UITableViewDataSource,ChartViewDelegate{
    //折线图
    
//    let bottomArr = ["一月", "二月", "三月", "四月","五月", "六月", "七月", "八月","九月", "十月", "十一月", "十二月",]
    var myType = ""
    let centerImageView1 = GIFImageView()
    var header = MJRefreshNormalHeader()
    var bottomLineView = UIView()
    var mylabel = UILabel()
    let aaChartView = AAChartView()
    var dataArr : NSArray?
    let tempBtn = UIButton(frame: CGRect(x: 0, y: 40, width: (kScreenWidth)/2 , height: 40))
    let tempBtn1 = UIButton(frame: CGRect(x: 0 + (kScreenWidth)/2*CGFloat(1), y: 40, width: (kScreenWidth)/2 , height: 40))
    var bottomArr : NSMutableArray?
    var priceArr : NSMutableArray?
    var chartView: LineChartView = {
        $0.noDataText = "暂无统计数据" //无数据的时候显示
        $0.chartDescription?.enabled = false //是否显示描述
        $0.scaleXEnabled = false
        $0.scaleYEnabled = false
        $0.leftAxis.drawGridLinesEnabled = false //左侧y轴设置，不画线
        $0.rightAxis.drawGridLinesEnabled = false //右侧y轴设置，不画线
        $0.rightAxis.drawAxisLineEnabled = false
        $0.rightAxis.enabled = false
        $0.legend.enabled = true
        return $0
    }(LineChartView())
    var myDic:Dictionary<String,Any>=[:]
    //myDic : NSDictionary = [:]
    override func viewWillAppear(_ animated: Bool) {
       
        getData()
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
   
   
    override func viewDidLoad() {
        super.viewDidLoad()
        layoutNav()
//        centerImageView1.animate(withGIFNamed: "01") {
            print("It's animating!")
//        }
    centerImageView1.animate(withGIFNamed: "01") {
            print("")
        }
        
        
        
//        var imgArray = [UIImage]();
//        for i in 0...84 {
//            let imageName = i < 10 ? "\("宝石")" + "0000\(i).tga" : "\("宝石")" + "000\(i).tga";
//            let image = UIImage(named: imageName)!;
//            imgArray.insert(image, at: i);
//        }
//        
//       
//        ///---2、将数组添加到UIImageView中
//        centerImageView1.animationImages = imgArray;
//        ///---3、设置动画持续时间
//        centerImageView1.animationDuration = Double(imgArray.count) * 0.1;
//        ///---4、设置动画的持续次数
//        centerImageView1.animationRepeatCount = 0;
//        ///---5、开启动画
//        centerImageView1.startAnimating();
        
        
        makeBaseUI()

        // Do any additional setup after loading the view.
    }
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
//    override func viewDidAppear(_ animated: Bool) {
//
//    }

    //MARK: - Private Methods
    
    func layoutNav() {
        //nav
        self.navigationController?.setNavigationBarHidden(true, animated: true); navigationController?.navigationBar.subviews[0].removeFromSuperview()//去
    }
    
    func makeBaseUI(){
        self.view.addSubview(myTableView)
        header.setRefreshingTarget(self, refreshingAction: #selector(SecondViewController.headerRefresh))
        
        self.myTableView.mj_header = header
        
        
        //getDataCir()
    }
    @objc func headerRefresh(){
        getData()
    }
    
    func getDataValue() {
        MyNetwork.request(target: .dayPriceWay(page: "1", limit: "30", type: "1"), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            if needModel.code == 1111{
                var tempDic:Dictionary<String,Any>=[:]
                tempDic = needModel.mydata as! Dictionary<String, Any>
                var tempDic1:Dictionary<String,Any>=[:]
                tempDic1 = tempDic["list"] as! Dictionary<String, Any>
                //                var tempArr : NSArray?
                //                tempArr = tempDic1["data"] as! NSArray
                var tempArr = NSMutableArray.yy_modelArray(with: productModel.self, json: tempDic1["data"]!)
                
                //                self.dataArr = tempDic1["data"] as! NSArray
                self.bottomArr = NSMutableArray()
                self.priceArr = NSMutableArray()
                for model in tempArr! {
                    
                    let tempmodel:productModel = model as! productModel
                    self.bottomArr?.add(tempmodel.create_time)
                    
                    self.priceArr?.add(tempmodel.price as! Int)
                    
                }
                
                
                let aaChartModel = AAChartModel.init()
                    .chartType(AAChartType.Spline)//Can be any of the chart types listed under `AAChartType`.
                    .animationType(AAChartAnimationType.Bounce)
                    .title("")//The chart title
                    .subtitle("")//The chart subtitle
                    .dataLabelEnabled(false) //Enable or disable the data labels. Defaults to false
                    .tooltipValueSuffix("RMB")//the value suffix of the chart tooltip
                    .categories(self.bottomArr as! Array<Any>)
                    .colorsTheme(["#96DBF9","#06caf4","#7dffc0"])
                    .series([
                        AASeriesElement()
                            .name("DKE")
                            .data(self.priceArr as! Array<Any>)
                            .toDic()!,
                        ])
                self.aaChartView.aa_drawChartWithChartModel(aaChartModel)
                
                
                
                //
                
                
                //                self.navigationController?.pushViewController(ConfirmZhuCeViewController(), animated: true)
                
            }else{
                self.navigationController?.view.makeToast(XALanguageManager.getString(key: "失败"))
            }
            
        }) { (error) in
            //kLog(error.localizedDescription)
            self.navigationController?.view.makeToast(error.localizedDescription)
        }
    }
    func getDataCir() {
        MyNetwork.request(target: .dkeWay(page: "1", limit: "30", type: "1"), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            if needModel.code == 1111{
                var tempDic:Dictionary<String,Any>=[:]
                tempDic = needModel.mydata as! Dictionary<String, Any>
                var tempDic1:Dictionary<String,Any>=[:]
                tempDic1 = tempDic["list"] as! Dictionary<String, Any>
//                var tempArr : NSArray?
//                tempArr = tempDic1["data"] as! NSArray
                var tempArr = NSMutableArray.yy_modelArray(with: productModel.self, json: tempDic1["data"]!)

//                self.dataArr = tempDic1["data"] as! NSArray
                self.bottomArr = NSMutableArray()
                self.priceArr = NSMutableArray()
                for model in tempArr! {
                    
                    let tempmodel:productModel = model as! productModel
                    self.bottomArr?.add(tempmodel.days)
                    
                    self.priceArr?.add(tempmodel.number?.floatValue)
                    
                }
                
               
                let aaChartModel = AAChartModel.init()
                    .chartType(AAChartType.Spline)//Can be any of the chart types listed under `AAChartType`.
                    .animationType(AAChartAnimationType.Bounce)
                    .title("")//The chart title
                    .subtitle("")//The chart subtitle
                    .dataLabelEnabled(false) //Enable or disable the data labels. Defaults to false
                    .tooltipValueSuffix("")//the value suffix of the chart tooltip
                    .categories(self.bottomArr as! Array<Any>)
                    .colorsTheme(["#96DBF9","#06caf4","#7dffc0"])
                    .series([
                        AASeriesElement()
                            .name("DKE")
                            .data(self.priceArr as! Array<Any>)
                            .toDic()!,
                        ])
                self.aaChartView.aa_drawChartWithChartModel(aaChartModel)
                
                
                
//
                
            
//                self.navigationController?.pushViewController(ConfirmZhuCeViewController(), animated: true)
                
            }else{
                self.navigationController?.view.makeToast(XALanguageManager.getString(key: "失败"))
            }
            
        }) { (error) in
            //kLog(error.localizedDescription)
            self.navigationController?.view.makeToast(error.localizedDescription)
        }
    }
    
    func getData() {
        MyNetwork.request(target: .dkeXing(user_id: Defaults[.userID]!), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            self.header.endRefreshing()
            if needModel.code == 1111{
                //wk_user_number
                
                self.myDic = needModel.mydata as! Dictionary<String, Any>
                
                if (self.myDic["is_wk"] as! Int)==1{
                    self.centerImageView1.animate(withGIFNamed: "01") {
                        print("")
                    }
                    self.centerImageView1.startAnimatingGIF()
                }else{
                    self.centerImageView1.stopAnimatingGIF()
                    self.centerImageView1.image = UIImage.init(named: "0111－5（被拖移）")
                }
                
                
                
                
                self.mylabel.text = String(format: "%@...\n%@%@",XALanguageManager.getString(key: "DKE奋力开采中"),self.myDic["wk_user_number"] as! CVarArg,XALanguageManager.getString(key: "名星球居民正在开采"))
//                if UIDevice.
                
                //self.mylabel.isHidden = true
                self.myTableView.reloadData()
                
            }else{
               self.header.endRefreshing() //self.navigationController?.view.makeToast(needModel.message)
            }
            
        }) { (error) in
            //kLog(error.localizedDescription)
           self.header.endRefreshing()
            self.navigationController?.view.makeToast(error.localizedDescription)
        }
    }
    
    // 创建-

    //MARK: - tableView Delegate And DataSource
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        }
        
//        let titleLabel = UILabel(frame: CGRect(x: 15, y: 10, width: kScreenWidth, height: 17))
//        titleLabel.text = "DKE index"
//        cell?.contentView.addSubview(titleLabel)
//
//
//        let imageBtn = UIButton(frame: CGRect(x: kScreenWidth - 35, y: 15, width: 20, height: 20))
//        imageBtn.setImage(UIImage.init(named: "问号 (1)"), for: .normal)
//        cell?.contentView.addSubview(imageBtn)
//
////        chartView = LineChartView.init(frame: CGRect(x:0,y:80,width:kScreenWidth,height:320))
//////        chartView?.barData
////        // 签协议
////        chartView.delegate = self
////        chartView.backgroundColor = UIColor.white
////        cell?.contentView.addSubview(chartView)
////        setLineChartViewData(["123","123","123","123","123","123","123","123","123","123","123","123"], [15,31,53,3,1,53,53,23,23,64,23,43])
//
//
//        aaChartView.frame = CGRect(x:0,y:80,width:kScreenWidth,height:350)
//        aaChartView.scrollEnabled = false
//        cell?.contentView.addSubview(aaChartView)
//        cell?.selectionStyle = .none
//
//
//        let tempArr1 = [XALanguageManager.getString(key: "Circulation"),XALanguageManager.getString(key: "Value assessment")]
//
//        var i = 0
//        bottomLineView.frame = CGRect(x: 15, y: 80, width: kScreenWidth/2 - 30, height: 2)
//        bottomLineView.backgroundColor = UIColor("#374BFF")
//        cell?.contentView.addSubview(bottomLineView)
//
//        cell?.contentView.addSubview(tempBtn)
//        tempBtn.tag = i + 3000
//        tempBtn.isSelected = true
//        tempBtn.setTitleColor(UIColor("#374BFF"), for: .selected)
//        tempBtn.addTarget(self, action: #selector(centerAction(sender:)), for: .touchUpInside)
////        tempBtn.backgroundColor = UIColor("#F5F5F9")
//        tempBtn.setTitle(tempArr1[i], for: .normal)
//        tempBtn.setTitleColor(UIColor("#909090"), for: .normal)
//
//        i = 1
//
//        cell?.contentView.addSubview(tempBtn1)
//        tempBtn1.tag = i + 3000
//        tempBtn1.isSelected = false
//
//        tempBtn1.setTitleColor(UIColor("#374BFF"), for: .selected)
//        tempBtn1.addTarget(self, action: #selector(centerAction(sender:)), for: .touchUpInside)
////        tempBtn1.backgroundColor = UIColor("#F5F5F9")
//        tempBtn1.setTitle(tempArr1[i], for: .normal)
//        tempBtn1.setTitleColor(UIColor("#909090"), for: .normal)
//
////        for  i in 0...1 {
////
////            let tempBtn = UIButton(frame: CGRect(x: 0 + (kScreenWidth)/2*CGFloat(i), y: 40, width: (kScreenWidth)/2 , height: 40))
////            cell?.contentView.addSubview(tempBtn)
////            tempBtn.tag = i + 3000
////            tempBtn.isSelected = false
////
////            tempBtn.setTitleColor(UIColor("#374BFF"), for: .selected)
////            tempBtn.addTarget(self, action: #selector(centerAction(sender:)), for: .touchUpInside)
////            tempBtn.backgroundColor = UIColor("#F5F5F9")
////            tempBtn.setTitle(tempArr1[i], for: .normal)
////            tempBtn.setTitleColor(UIColor("#909090"), for: .normal)
////
////
////
////        }
//
//        let tempArr = ["All","year","month","week"]
//        for  i in 0...3 {
//
//            let tempBtn = UIButton(frame: CGRect(x: 0 + (kScreenWidth)/4*CGFloat(i), y: 430, width: (kScreenWidth)/4 , height: 40))
//            cell?.contentView.addSubview(tempBtn)
//            tempBtn.tag = i + 2000
//            tempBtn.addTarget(self, action: #selector(centerAction(sender:)), for: .touchUpInside)
//            tempBtn.backgroundColor = UIColor("#F5F5F9")
//            tempBtn.setTitle(tempArr[i], for: .normal)
//            tempBtn.setTitleColor(UIColor("#909090"), for: .normal)
//        }
//
//        cell?.textLabel?.text = ""
        return cell!
    }
    //footer height
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    // row height
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    // header height
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return self.view.frame.height
        }else{
            return CGFloat.leastNormalMagnitude
        }
        
    }
    
    //配置折线图
    func setLineChartViewData(_ dataPoints: [String], _ values: [Double]) {
        
        let xAxis = chartView.xAxis
        xAxis.labelPosition = .bottom //x轴的位置
        xAxis.labelFont = .systemFont(ofSize: 10)
        xAxis.drawGridLinesEnabled = false
        xAxis.granularity = 1.0
        xAxis.valueFormatter = self
        
        var dataEntris: [ChartDataEntry] = []
        for (idx, _) in dataPoints.enumerated() {
            let dataEntry = ChartDataEntry(x: Double(idx), y: values[idx])
            dataEntris.append(dataEntry)
        }
        let lineChartDataSet = LineChartDataSet(values: dataEntris, label: "")
        //外圈
        lineChartDataSet.setCircleColor(UIColor.clear)
        //内圈
        lineChartDataSet.circleHoleColor = UIColor("#E8705E")
        //线条显示样式
        lineChartDataSet.colors = [UIColor("#E8705E")] as! [NSUIColor]
        
        let lineChartData = LineChartData(dataSet: lineChartDataSet)
        chartView.data = lineChartData
        
        //设置x轴样式
        let xFormatter = IndexAxisValueFormatter()
        xFormatter.values = dataPoints
        
        self.chartView.animate(xAxisDuration: 0.4)
    }
    
    func loadGifInLocal(gifName: String?) -> Data? {
        guard gifName != nil else {
            return nil
        }
        
        let gifPath = Bundle.main.path(forResource: gifName, ofType: "gif")
        guard gifPath != nil else {
            print("文件名为" + gifName! + "的gif图不存在！")
            return nil
        }
        var gifData: Data = Data.init()
        do {
            gifData = try Data.init(contentsOf: URL.init(fileURLWithPath: gifPath!))
        } catch {
            print(error)
        }
        return gifData
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let backView = UIView()
        backView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth)
    backView.addSubview(centerImageView1)
        let titleArr = [XALanguageManager.getString(key: "Recharge M"),XALanguageManager.getString(key: "Obtain N")]
        let ImageArr = ["时间","文件"]
        
        let backImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: self.view.frame.height ))
        
//        backImageView.image = UIImage.sd_image(withGIFData: loadGifInLocal(gifName: "750像素"))
        
        backView.backgroundColor = UIColor.black
        backImageView.image = UIImage.init(named: "bg@2x.jpg")
        
        
        let centerBackView = UIView(frame: CGRect(x: 15, y: self.view.frame.height - 140, width: kScreenWidth - 30, height: 80))
        centerBackView.backgroundColor = UIColor("#171F3C")
        centerBackView.alpha = 1;
        centerBackView.layer.cornerRadius = 4
        backView.addSubview(centerBackView)
        //self.view.bringSubview(toFront: backImageView)
        backView.addSubview(backImageView)
        
        let navtitleLabel = UILabel()
        backView.addSubview(navtitleLabel)
        navtitleLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(backView)
            make.top.equalTo(30)
            make.size.equalTo(CGSize(width: 350, height: 40))
        }
        
        navtitleLabel.text = XALanguageManager.getString(key: "")
        navtitleLabel.textColor = UIColor.white
        navtitleLabel.textAlignment = .center
        navtitleLabel.font = UIFont.boldSystemFont(ofSize: 15)
        
        
        let titleLabel = UILabel()
        backView.addSubview(titleLabel)
        titleLabel.frame = CGRect(x: 15, y: 90*_kScreenWidth, width: kScreenWidth-30, height: 40)
        titleLabel.text = XALanguageManager.getString(key: "Obtain N")
        titleLabel.adjustsFontSizeToFitWidth = true
        titleLabel.textColor = UIColor.white
        titleLabel.textAlignment = .left
        titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
        var attrStr1 : NSAttributedString = NSAttributedString(string: "  0.0")
//        self.myDic
        if self.myDic.count > 0 {
            attrStr1 = NSAttributedString(string: String(format: "  %@",self.myDic["dke"]! as! CVarArg))
        }
        
        var attrDkeStr2 : NSAttributedString = NSAttributedString(string: "  0.0")
        //        self.myDic
        if self.myDic.count > 0 {
            attrDkeStr2 = NSAttributedString(string: String(format: "   % @",self.myDic["baozhi_dke"]! as! CVarArg))
        }

        // 图文混排
        let attacment = NSTextAttachment()
        attacment.image = UIImage(named: "THTC币@2x")   // 设置要显示的图片
        let font = titleLabel.font                       // 取出demoLabel控制文字大小
        
        let attacment1 = NSTextAttachment()
        attacment1.image = UIImage(named: "1431539847334_.pic_hd.jpg")
        attacment1.bounds = CGRect(x: 5, y: 0, width: 12, height: 12)
        
        attacment.bounds = CGRect(x: 0, y: -4, width: (font?.lineHeight)!, height: (font?.lineHeight)!)
        
        let attacment1_temp = NSTextAttachment()
        attacment1_temp.image = UIImage(named: "锁@2x")
        attacment1_temp.bounds = CGRect(x: 5, y: 0, width: 12, height: 12)
        
        attacment.bounds = CGRect(x: 0, y: -4, width: (font?.lineHeight)!, height: (font?.lineHeight)!)
        
        
        let attrImageStr = NSAttributedString(attachment: attacment)
        let attrImageStr1 = NSAttributedString(attachment: attacment1)
        let attrImageStr1_temp = NSAttributedString(attachment: attacment1_temp)
        let attrMStr = NSMutableAttributedString()
        
        
        
        attrMStr.append(attrImageStr)
        attrMStr.append(attrStr1)
        
        attrMStr.append(attrImageStr1_temp)
        attrMStr.append(attrDkeStr2)
        
        attrMStr.append(attrImageStr1)
        
        titleLabel.attributedText = attrMStr
        
        let titleLabel1 = UILabel()
        backView.addSubview(titleLabel1)
        titleLabel1.frame = CGRect(x: 15, y: 90*_kScreenWidth + 40, width: kScreenWidth-30, height: 40)
        titleLabel1.text = XALanguageManager.getString(key: "Obtain N")
        titleLabel1.adjustsFontSizeToFitWidth = true
        titleLabel1.textColor = UIColor.white
        titleLabel1.textAlignment = .left
        titleLabel1.font = UIFont.boldSystemFont(ofSize: 17)
        var attrStr2 : NSAttributedString = NSAttributedString(string: " 0.0")
        if self.myDic.count > 0 {
            attrStr2 = NSAttributedString(string: String(format: "  %@",self.myDic["m"]! as! CVarArg))
        }
        
        // 图文混排
        let attacment2 = NSTextAttachment()
        attacment2.image = UIImage(named: "组 1@2x")   // 设置要显示的图片
        let font2 = titleLabel1.font                       // 取出demoLabel控制文字大小
        attacment2.bounds = CGRect(x: 0, y: -4, width: (font2?.lineHeight)!, height: (font2?.lineHeight)!)
        let attrImageStr2 = NSAttributedString(attachment: attacment2)
        let attrMStr2 = NSMutableAttributedString()
        
        
        var attrMStrbaozhi : NSAttributedString = NSAttributedString(string: "  0.0")
        //        self.myDic
        if self.myDic.count > 0 {
            attrMStrbaozhi = NSAttributedString(string: String(format: "  %  @",self.myDic["baozhi_m"]! as! CVarArg))
        }
        
        
        let attacmentM_baozhi = NSTextAttachment()
        attacmentM_baozhi.image = UIImage(named: "锁@2x")
        attacmentM_baozhi.bounds = CGRect(x: 5, y: 0, width: 12, height: 12)
        let attacmentM_baozhi2 = NSAttributedString(attachment: attacmentM_baozhi)
        
        attrMStr2.append(attrImageStr2)
        attrMStr2.append(attrStr2)
        attrMStr2.append(attacmentM_baozhi2)
        attrMStr2.append(attrMStrbaozhi)
        
        attrMStr2.append(attrImageStr1)

        titleLabel1.attributedText = attrMStr2
        
        
        
        let titleLabel2 = UILabel()
        backView.addSubview(titleLabel2)
        titleLabel2.frame = CGRect(x: 15, y: 90*_kScreenWidth + 80, width: kScreenWidth-30, height: 40)
        titleLabel2.text = XALanguageManager.getString(key: "Obtain N")
        titleLabel2.adjustsFontSizeToFitWidth = true
        titleLabel2.textColor = UIColor.white
        titleLabel2.textAlignment = .left
        titleLabel2.font = UIFont.boldSystemFont(ofSize: 17)
        var attrStr3 : NSAttributedString = NSAttributedString(string: " 0.0")
        var attrStr4 : NSAttributedString = NSAttributedString(string: " 0.0")
        if self.myDic.count > 0 {
            attrStr3 = NSAttributedString(string: String(format: "  %@",self.myDic["n"]! as! CVarArg))
            attrStr4 = NSAttributedString(string: String(format: "  %@",self.myDic["baozhi_n"]! as! CVarArg))
            
            
        }
        
        // 图文混排
        let attacment3 = NSTextAttachment()
        attacment3.image = UIImage(named: "组 6@2x")   // 设置要显示的图片
        let font3 = titleLabel1.font                       // 取出demoLabel控制文字大小
        attacment3.bounds = CGRect(x: 0, y: -4, width: (font3?.lineHeight)!, height: (font3?.lineHeight)!)
        let attrImageStr3 = NSAttributedString(attachment: attacment3)
        let attrMStr3 = NSMutableAttributedString()
        attrMStr3.append(attrImageStr3)
        attrMStr3.append(attrStr3)
        attrMStr3.append(attrImageStr1_temp)
        attrMStr3.append(attrStr4)
        attrMStr3.append(attrImageStr1)
        
        titleLabel2.attributedText = attrMStr3
        let myButton = UIButton()
        backView.addSubview(myButton)
        myButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(backView.snp.bottom).offset(-170)
            make.right.equalTo(backView).offset(-20)
            make.size.equalTo(CGSize(width: 90, height: 30))
        }
        
        let topBtn01 = UIButton(frame: CGRect(x: 15, y: 90*_kScreenWidth, width: kScreenWidth-150, height: 40))
        
        let topBtn02 = UIButton(frame: CGRect(x: 15, y: 90*_kScreenWidth + 40, width: kScreenWidth-150, height: 40))
        let topBtn03 = UIButton(frame: CGRect(x: 15, y: 90*_kScreenWidth + 80, width: kScreenWidth-150, height: 40))
        
        topBtn01.titleLabel?.adjustsFontSizeToFitWidth = true
        topBtn02.titleLabel?.adjustsFontSizeToFitWidth = true
        topBtn03.titleLabel?.adjustsFontSizeToFitWidth = true
        
        
        backView.addSubview(topBtn01)
        backView.addSubview(topBtn02)
        backView.addSubview(topBtn03)
        topBtn01.addTarget(self, action: #selector(SecondViewController.top1Action), for: .touchUpInside)
         topBtn02.addTarget(self, action: #selector(SecondViewController.top2Action), for: .touchUpInside)
         topBtn03.addTarget(self, action: #selector(SecondViewController.top3Action), for: .touchUpInside)

        myButton.layer.cornerRadius = 15
        myButton.backgroundColor = UIColor("#3E3F41")
        myButton.alpha = 0.7
        myButton.setTitle(XALanguageManager.getString(key: "Charge"), for: .normal)
        myButton.isHidden = true
        myButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        myButton.tag = 4000
        myButton.addTarget(self, action: #selector(centerAction(sender:)), for: .touchUpInside)
        
        
        let centerImageView = UIImageView()
        centerImageView.image = UIImage.init(named: "")
        backView.addSubview(centerImageView)
        centerImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(backView)
            make.top.equalTo(titleLabel2.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 180 * _kScreenWidth, height: 180 * _kScreenWidth))
        }
        centerImageView.isHidden = true
        
        
        
        mylabel.numberOfLines = 0
        mylabel.font = UIFont.systemFont(ofSize: 14)
        mylabel.textColor = UIColor.white
        
        backView.addSubview(mylabel)
        mylabel.textAlignment = .center
        mylabel.snp.makeConstraints { (make) in
            make.top.equalTo(centerImageView1.snp.bottom)
            make.centerX.equalTo(backView)
            make.size.equalTo(CGSize(width: 300, height: 50))
        }
        
        
        
        
        
        
        
        //
        
//        centerImageView1.image = UIImage.init(named: "色相_饱和度 6@2x")
        
        //let queue1 = DispatchQueue.global()
        // 异步任务
        // 任务1
//        queue1.async {
//            centerImageView1.image = UIImage.sd_image(withGIFData: self.loadGifInLocal(gifName: "01"))
        
        
            centerImageView1.snp.makeConstraints { (make) in
                make.centerX.equalTo(backView)
                make.centerY.equalTo(centerImageView)
                make.size.equalTo(CGSize(width: 230 * _kScreenWidth, height: 230 * _kScreenWidth))
            }
//        }

        let myButtonRight = UIButton()
        backView.addSubview(myButtonRight)
        myButtonRight.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.top).offset(10)
            make.right.equalTo(backView).offset(0)
            make.size.equalTo(CGSize(width: 100, height: 20))
        }
        myButtonRight.setImage(UIImage.init(named: "铲子3@2x"), for: .normal)
        myButtonRight.imageView?.contentMode = .scaleAspectFit
        myButtonRight.setTitle(XALanguageManager.getString(key: "  释放进度"), for: .normal)
        myButtonRight.titleLabel?.adjustsFontSizeToFitWidth = true
        myButtonRight.layer.cornerRadius = 10
        myButtonRight.backgroundColor = UIColor("#6C93F6")
//        myButtonRight.backgroundColor = UIColor("#217AFF")
        myButtonRight.alpha = 1
        myButtonRight.titleLabel?.font = UIFont.systemFont(ofSize: 12)
//        myButtonRight.setTitle("1233", for: .normal)
        myButtonRight.addTarget(self, action: #selector(SecondViewController.gotoKuangViewController), for: .touchUpInside)

        
        let myButton1 = UIButton()
        backView.addSubview(myButton1)
        myButton1.snp.makeConstraints { (make) in
            make.top.equalTo(myButtonRight.snp.bottom).offset(20)
            make.right.equalTo(backView).offset(0)
            make.size.equalTo(CGSize(width: 100, height: 20))
        }
        myButton1.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        myButton1.setImage(UIImage.init(named: "盾牌@2x"), for: .normal)
        myButton1.imageView?.contentMode = .scaleAspectFit
        myButton1.setTitle(XALanguageManager.getString(key: "  立即保值"), for: .normal)
        myButton1.titleLabel?.adjustsFontSizeToFitWidth = true
        myButton1.layer.cornerRadius = 10
        myButton1.backgroundColor = UIColor("#F87A78")
        myButton1.alpha = 1
        myButton1.addTarget(self, action: #selector(SecondViewController.gotoPrevationViewController), for: .touchUpInside)
        
        for  i in 0...1 {
            
            let tempBtn = UIButton(frame: CGRect(x:  (kScreenWidth - 30)/4*CGFloat(i*2+1)-(kScreenWidth - 30)/8, y: 0, width: (kScreenWidth - 30)/4 , height: 80))
            centerBackView.addSubview(tempBtn)
            tempBtn.tag = i + 1000
            tempBtn.addTarget(self, action: #selector(centerAction(sender:)), for: .touchUpInside)
            tempBtn.backgroundColor = UIColor.clear
            
            let tempImageView = UIImageView()
            tempImageView.image = UIImage.init(named: ImageArr[i])
            tempBtn.addSubview(tempImageView)
            tempImageView.snp.makeConstraints { (make) in
                make.centerX.equalTo(tempBtn)
                make.size.equalTo(CGSize(width: 40, height: 40))
                make.centerY.equalTo(tempBtn).offset(-10)
            }
            let tempLabel = UILabel()
            tempLabel.text = titleArr[i]
            tempLabel.font = UIFont.systemFont(ofSize: 13)
            tempLabel.textAlignment = .center
            tempLabel.adjustsFontSizeToFitWidth = true
            tempLabel.textColor = UIColor("909090")
            tempBtn.addSubview(tempLabel)
            tempLabel.snp.makeConstraints { (make) in
                make.centerX.equalTo(tempBtn)
                make.size.equalTo(CGSize(width: 100, height: 15))
                make.bottom.equalTo(tempBtn).offset(-10)
            }
            

        }

        if section == 0 {
            return backView
        }else{
            return nil
        }
//        return backView
    }
    @objc func top3Action() {
        if Defaults[.isLogInKey] == false{
            super.LogIn()
            return
        }
        
        let RecVC = KuangListViewController()
        RecVC.isFrom = "N"
        self.navigationController?.pushViewController(RecVC, animated: true)
    }
    @objc func top1Action() {
        if Defaults[.isLogInKey] == false{
            super.LogIn()
            return
        }
        
        let RecVC = KuangListViewController()
        RecVC.isFrom = "DKE"
        self.navigationController?.pushViewController(RecVC, animated: true)
    }
    @objc func top2Action() {
        if Defaults[.isLogInKey] == false{
            super.LogIn()
            return
        }
        
        let RecVC = KuangListViewController()
        RecVC.isFrom = "M"
        self.navigationController?.pushViewController(RecVC, animated: true)
    }
    
    @objc func gotoPrevationViewController(){
        if Defaults[.isLogInKey] == false{
            super.LogIn()
            return
        }
        self.navigationController?.pushViewController(PrevationV2ViewController(), animated: true)
    }
    @objc func gotoKuangViewController(){
        
        if Defaults[.isLogInKey] == false{
            super.LogIn()
            return
        }
        self.navigationController?.pushViewController(KuangViewController(), animated: true)
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return nil
    }
    
    // row num
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    // 组数
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    //点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    override func willMove(toParentViewController parent: UIViewController?) {
        
    }
    
    // MARK: -Button Action
    @objc func centerAction(sender:UIButton){
        if Defaults[.isLogInKey] == false{
            super.LogIn()
            return
        }
        
        let target = sender.tag
        switch target {
        case 1000:
            print("1000")
        self.navigationController?.pushViewController(RechargeListViewController(), animated: true)
        case 1001:
           
           let vc = ObtainNViewController()
//           if self.myDic.count > 0{
//
//           }
//           vc.nNumber = String(format: "  %@",self.myDic["tg_n"]! as! CVarArg) as NSString
           self.navigationController?.pushViewController(vc, animated: true)
            
            print("1001")
        case 1002:
            let MyWebVC = MyBaseWebViewController()
            MyWebVC.title = XALanguageManager.getString(key: "Rule introduction");
            MyWebVC.urlStr = "http://renedescartes.io/web/home/accelerate"
            self.navigationController?.pushViewController(MyWebVC, animated: true)
            
            print("1002")
            
        case 2000:
            print("2000")
            
        case 2001:
            print("2001")
        case 2002:
            print("2002")
        case 2003:
            print("2003")
        case 3000:
            getDataCir()
            sender.isSelected = true
            bottomLineView.frame = CGRect(x: 15, y: 80, width: kScreenWidth/2 - 30, height: 2)
            (self.view.viewWithTag(3001) as! UIButton).isSelected = false
//            button.isSelected = false
            print("3000")
        case 3001:
            getDataValue()
            bottomLineView.frame = CGRect(x:kScreenWidth/2 + 15, y: 80, width: kScreenWidth/2 - 30, height: 2)
            sender.isSelected = true
            (self.view.viewWithTag(3000) as! UIButton).isSelected = false
//            button.isSelected = false
            print("3001")
        case 3002:
            print("3002")
        case 4000:
self.navigationController?.pushViewController(MyteamViewController(), animated: true)

        default:
            print("123")
        }
    }
    
    
    
    
    // MARK: - 懒加载
    private lazy var myTableView:UITableView = {
        let myTableView:UITableView
        if UIDevice.current.isX(){
            myTableView = UITableView(frame: CGRect(x: 0, y: -44, width: kScreenWidth, height: self.view.frame.height + 44), style: UITableViewStyle.grouped)
        }else{
            myTableView = UITableView(frame: CGRect(x: 0, y: -20, width: kScreenWidth, height: self.view.frame.height + 5), style: UITableViewStyle.grouped)
        }
        myTableView.backgroundColor = UIColor.black
        myTableView.separatorStyle = .none
        myTableView.delegate = self
        myTableView.dataSource = self
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return myTableView
    }()

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
